<template>
  <view class="chat-header">
    <view class="header-left">
      <view class="header-icon back-icon" @tap="goBack">
        <image src="/static/images/doubao_icon/back_chat.png" mode="aspectFit" class="icon-back"></image>
      </view>
    </view>
    <view class="header-center">
      <view class="title-container">
        <text class="header-title">{{title}}</text>
        <view class="voice-icon" @tap="toggleSpeaker">
          <image 
            :src="speakerEnabled ? '/static/images/doubao_icon/voice_playing.png' : '/static/images/doubao_icon/voice_play.png'" 
            mode="aspectFit"
            :class="['icon-image', isVoicePlaying ? 'icon-playing' : '']"
          ></image>
        </view>
      </view>
      <text class="header-subtitle">{{subtitle}}</text>
    </view>
    <view class="header-right">
    </view>
  </view>
</template>

<script>
export default {
  name: 'ChatHeader',
  props: {
    title: {
      type: String,
      default: '新如影'
    },
    subtitle: {
      type: String,
      default: 'xryai.cn'
    },
    speakerEnabled: {
      type: Boolean,
      default: false
    },
    isVoicePlaying: {
      type: Boolean,
      default: false
    },
    isLoggedIn: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    goBack() {
      this.$emit('go-back');
    },
    toggleSpeaker() {
      this.$emit('toggle-speaker');
    },
    openHeaderMenu() {
      this.$emit('open-header-menu');
    },
    goToLogin() {
      console.log('ChatHeader组件中的goToLogin方法被调用');
      
      if (this.isLoggedIn) {
        // 如果已登录，显示退出登录操作菜单
        uni.showActionSheet({
          itemList: ['退出登录'],
          success: (res) => {
            if (res.tapIndex === 0) {
              // 确认退出
              uni.showModal({
                title: '提示',
                content: '确定要退出登录吗？',
                success: (modalRes) => {
                  if (modalRes.confirm) {
                    // 清除本地缓存的登录状态
                    uni.removeStorageSync('isLoggedIn');
                    uni.removeStorageSync('username');
                    
                    // 通知父组件跳转到登录页
                    this.$emit('go-to-login');
                  }
                }
              });
            }
          }
        });
      } else {
        // 如果未登录，直接跳转
        this.$emit('go-to-login');
      }
    }
  }
}
</script>

<style>
.chat-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 28px;
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  height: 80px;
  padding-top: var(--status-bar-height);
  border-bottom-left-radius: 12px;
  border-bottom-right-radius: 12px;
}

.header-left {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 10px;

}

.header-icon {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-back, .icon-image {
  width: 24px;
  height: 24px;
}

.back-icon {
  padding: 6px;
  margin-top: 10px;

}

.header-center {
  flex: 1;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.title-container {
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-title {
  font-size: 22px;
  font-weight: bold;
  color: #222;
  text-align: center;
  margin-right: 8px;
  margin-top: 30px;

}

.voice-icon {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 25px;
}

.icon-playing {
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.2);
    opacity: 0.8;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.header-subtitle {
  font-size: 13px;
  color: #999;
  margin-top: 0px;
}

.header-right {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;

}
</style>

<style>
/* 添加聊天框上边距，避免被导航栏遮挡 */
page {
  --chat-header-height: calc(80px + var(--status-bar-height));
}

/* 聊天内容容器样式，确保不被导航栏遮挡 */
.chat-container {
  padding-top: var(--chat-header-height);
}
</style> 